#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "lc_exception.h"
#include "lc_array.h"
#include "lc_memory.h"


LCarray *lc_memory = NULL;

void lcMemoryInit(void)
{
    lc_memory = (LCarray*) malloc (sizeof (LCarray));
    lc_memory->memory = NULL;
    lc_memory->allocated = 0;
    lc_memory->used = 0;
    lc_memory->index = -1;
}

void lcMemoryFree(void)
{
    lc_memory->allocated = 0;
    lcArrayFree(lc_memory);
}

void* lcMalloc(size_t size)
{
    if (lc_memory == NULL) {
        lcMemoryInit();
    }

    void* block = malloc(size);

    if (block == NULL) {
        THROW_OUTOFMEMORY_EXCEPTION((int) lc_memory->allocated);
    }
    lcArrayAdd(lc_memory, block);
    return block;
}

size_t lcCountMemoryBlock(void)
{
    return lc_memory->index;
}

size_t lcCountMemoryAllocated(void)
{
    return lc_memory->allocated;
}

size_t lcCountMemoryUsed(void)
{
    return lc_memory->used;
}